package com.glory.hikvisionmobile.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.bean.TrackIn.XPdaDieboundMappingBean;
import com.glory.hikvisionmobile.utils.EditTextEnterUtils;
import java.util.List;

public class BindCarrierListAdapter extends BaseQuickAdapter<XPdaDieboundMappingBean, BaseViewHolder> {

    public BindCarrierListAdapter(@Nullable List<XPdaDieboundMappingBean> data) {
        super(R.layout.adapter_bind_carrier_lsit,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, XPdaDieboundMappingBean item) {
        helper.setText(R.id.tvCarrierSeq,item.getCarrierSeq());
        EditText etTubeShellId = helper.getView(R.id.etTubeShellId);
        etTubeShellId.setText(item.getTubeShellId());
        setEditListener(etTubeShellId,item);

    }

    private void setEditListener(EditText editText, final XPdaDieboundMappingBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    if (s.length() > 0) {
                        item.setTubeShellId(s.toString());
                        EditTextEnterUtils.nextEditTextFocus(editText);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setTubeShellId(s.toString());


            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);

    }
}
